package com.zhss.massivedata.order.sharding.algorithm;

import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;
import org.springframework.stereotype.Component;

import java.util.Collection;

@Component
public class OrderShardingAlgorithm implements PreciseShardingAlgorithm<Long> {

    @Override
    public String doSharding(Collection<String> tables, PreciseShardingValue<Long> preciseShardingValue) {
        Long orderInfoId = preciseShardingValue.getValue();
        Long tableSuffix = orderInfoId / 32 % 32;
        for(String table : tables) {
            if(table.endsWith(String.valueOf(tableSuffix))) {
                return table;
            }
        }
        return null;
    }
}
